//
//  ZHPayWebPage.m
//  BCRouteKit
//
//  Created by chun.chen on 2019/8/8.
//

#import "ZHPayWebPage.h"
#import "BCRouter.h"
#import "BCRouteUtils.h"
#import <WebKit/WebKit.h>
#import <BCFileLog/BCFileLog.h>
#import <BCComConfigKit/BCComConfigKit.h>
#import <BCFoundation/NSString+BCHelper.h>

@interface ZHPayWebPage ()
/// 回调urlzzzz
@property (nonatomic, strong) NSString  *payBackurl;
@end

@implementation ZHPayWebPage

#pragma mark - system
+(void)load
{
    BCRouterRegist(kZHRoute_WebPayPage, NSStringFromClass([self class]));
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    NSString *requestUrl = navigationAction.request.URL.absoluteString;
    WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyCancel;
    // 这里命名随意防止关键字审查
    NSString *aaape = @"QCJhbGlwYXlzOi8vIg==";
    NSString *aaapd = [aaape bc_base64DecodedString];
    
    NSString *aaase = @"YWxpcGF5Oi8v";
    NSString *aaasd = [aaase bc_base64DecodedString];
    
    NSString *wwwhe = @"aHR0cHM6Ly93eC50ZW5wYXkuY29tL2NnaS1iaW4vbW1wYXl3ZWItYmluL2NoZWNrbXdlYg==";
    NSString *wwwhd = [wwwhe bc_base64DecodedString];
    
    NSString *wwwpe = @"d2VpeGluOi8vd2FwL3BheQ==";
    NSString *wwwpd = [wwwpe bc_base64DecodedString];
    
    if ([requestUrl hasPrefix:aaapd] || [requestUrl hasPrefix:aaasd]) {
        NSString *replaceUrl = [NSString stringWithFormat:@"%@",requestUrl];
        NSString *llle = @"YWxpcGF5cw==";
        NSString *llld = [llle bc_base64DecodedString];
        
        if ([replaceUrl containsString:@"fromAppUrlScheme"] && [replaceUrl containsString:llld]) {
            NSAssert(BCComConfig.config.setting[@"public_scheme"], @"请在BCComConfig配置APP public_scheme");
            replaceUrl = [replaceUrl stringByReplacingOccurrencesOfString:llld withString:BCComConfig.config.setting[@"public_scheme"]];
        }
        //NOTE: YWxpcGF5cw App
        BOOL isSucc = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:replaceUrl]];
        if (!isSucc) {
            NSLog(@"用户未安装zzzzzzzz端");
        }
    }else if ([requestUrl hasPrefix:wwwhd]) {
        NSString *rrre = @"UmVmZXJlcg==";
        NSString *rrrd = [rrre bc_base64DecodedString];
        NSMutableString *relReferer = [NSMutableString stringWithString:self.headers[rrrd]];
        
        if ([relReferer containsString:@"http://"]) {
            NSRange httpRange = [relReferer rangeOfString:@"http://"];
            [relReferer replaceCharactersInRange:httpRange withString:@""];
        }else if ([relReferer containsString:@"https://"]) {
            NSRange httpsRange = [relReferer rangeOfString:@"https://"];
            [relReferer replaceCharactersInRange:httpsRange withString:@""];
        }
        [relReferer appendString:@"://"];
        
        if ([navigationAction.request.allHTTPHeaderFields[rrrd] isEqualToString:relReferer]) {
            /// Referer格式正确
            decisionHandler(WKNavigationActionPolicyAllow);
        }else {
            // 设置Referer
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSURL *url = [navigationAction.request URL];
                    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
                    [request setHTTPMethod:@"GET"];
                    [request setValue:relReferer forHTTPHeaderField:rrrd];
                    [self.webView loadRequest:request];
                });
            });
            decisionHandler(WKNavigationActionPolicyCancel);
        }
        return;
    }else if ([requestUrl hasPrefix:wwwpd]) {
        BOOL isSucc = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:requestUrl]];
        if (!isSucc) {
        }
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }else if (self.payBackurl.length>0 && [requestUrl hasPrefix:self.payBackurl]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:kBCRouteNotifcation_PaySucc object:nil];
        [self.navigationController popViewControllerAnimated:NO];
        
    } else {
        actionPolicy = WKNavigationActionPolicyAllow;
    }
    decisionHandler(actionPolicy);
}
@end
